//
//  YSGWorngAlertViewController.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2022/5/10.
//  Copyright © 2022 tao Tao. All rights reserved.
//

import UIKit

extension YSGWrongAlertViewController {
    public class func show(title: String,
                           describe: String,
                           needTextConfirm: String? = nil,
                           viewController: UIViewController? = AppDelegate.share()?.rootVc,
                           complete: @escaping () -> Void) {
        let vc = YSGWrongAlertViewController(title: title, describe: describe, needTextConfirm: needTextConfirm, complete: complete)
        vc.modalPresentationStyle = .custom
        viewController?.showDetailViewController(vc, sender: nil)
    }
}

class YSGWrongAlertViewController: CLBaseViewController {
    private let complete: () -> Void
    private let describe: String
    private let needTextConfirm: String?
    private init(title: String,
                 describe: String,
                 needTextConfirm: String?,
                 complete: @escaping () -> Void) {
        self.complete = complete
        self.describe = describe
        self.needTextConfirm = needTextConfirm
        super.init()
        self.title = title
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var textField: UITextField?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initLayout()
    }
    
    private func initLayout() {

        view.backgroundColor = .black.withAlphaComponent(0.49)
        
        let contenView = UIView()
        contenView.backgroundColor = .white
        contenView.layer.cornerRadius = 10
        contenView.layer.masksToBounds = true
        view.addSubview(contenView)
        contenView.snp.makeConstraints { make in
            make.centerY.centerX.equalToSuperview()
            make.left.equalTo(30)
            make.right.equalTo(-30)
        }
        
        let title = UILabel()
        contenView.addSubview(title)
        title.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(40)
        }
        title.text = self.title
        title.font = .medium(18)
        title.textColor = .textDark
        
        let describe = UILabel()
        contenView.addSubview(describe)
        describe.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(50)
            make.right.equalTo(-50)
            make.top.equalTo(title.snp.bottom).offset(11)
        }
        describe.numberOfLines = 0
        describe.font = .regular(15)
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineSpacing = 8
        paragraph.alignment = .center
        let attri = NSAttributedString(string: self.describe, attributes: [.paragraphStyle: paragraph, .foregroundColor: UIColor.textGray])
        describe.attributedText = attri
        
        
        if let needTextConfirm = needTextConfirm {
            let textField = UITextField()
            contenView.addSubview(textField)
            textField.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.width.equalTo(220)
                make.top.equalTo(describe.snp.bottom).offset(10)
                make.height.equalTo(40)
            }
            textField.layer.cornerRadius = 5
            textField.layer.masksToBounds = true
            textField.layer.borderColor = UIColor.lightGreen.cgColor
            textField.layer.borderWidth = 0.5
            textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
            textField.leftViewMode = .always
            textField.font = .medium(15)
            textField.placeholder = "请输入\"\(needTextConfirm)\""
            self.textField = textField
        }

        
        let cancel = UIButton()
        contenView.addSubview(cancel)
        cancel.snp.makeConstraints { make in
            make.right.equalTo(contenView.snp.centerX).offset(-25)
            if let textField = textField {
                make.top.equalTo(textField.snp.bottom).offset(30)
            } else {
                make.top.equalTo(describe.snp.bottom).offset(30)
            }
            make.bottom.equalTo(-30)
            make.height.equalTo(34)
            make.width.equalTo(65)
        }
        cancel.layer.cornerRadius = 17
        cancel.layer.masksToBounds = true
        cancel.setTitle("取消", for: .normal)
        cancel.backgroundColor = .init(hex: "#A1D0C8")
        cancel.titleLabel?.font = .medium(14)
        cancel.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        
        let complete = UIButton()
        contenView.addSubview(complete)
        complete.snp.makeConstraints { make in
            make.left.equalTo(contenView.snp.centerX).offset(25)
            make.centerY.width.height.equalTo(cancel)
        }
        complete.layer.cornerRadius = 17
        complete.layer.masksToBounds = true
        complete.setTitle("确定", for: .normal)
        complete.backgroundColor = .mainColor
        complete.titleLabel?.font = .medium(14)
        complete.addTarget(self, action: #selector(completeClick), for: .touchUpInside)
    }
    
    @objc private func cancelClick() {
        self.dismiss(animated: true)
    }
    
    @objc private func completeClick() {
        if let needTextConfirm = needTextConfirm {
            if textField?.text != needTextConfirm {
                HUDTool.showError(message: "请输入\"\(needTextConfirm)\"")
                return
            }
        }
        complete()
        self.dismiss(animated: true)
    }
}
